package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliyunOSSUtils;
import com.itheima.utils.bean.AliyunOSSProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传Controller
 */
@Slf4j
@RestController
public class UploadController {
    // 配置阿里云OSS的一些基础配置信息
    // private static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";
    // private static final String bucketName = "java200-web-tlias";
    // @Value("${aliyun.oss.endpoint}")
    // private String endpoint;
    // @Value("${aliyun.oss.bucketName}")
    // private String bucketName;
    @Autowired
    private AliyunOSSProperties aliyunOSSProperties;

    @PostMapping("/upload")
    public Result upload(MultipartFile file) throws Exception {
        log.info("文件上传：{}", file);
        // 获取原始文件名
        String originalFilename = file.getOriginalFilename();
        // 获取文件拓展名
        String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
        log.info("文件拓展名：{}", extName);
        // 获取文件上传中阿里云OSS的endpoint和bucketName
        // 将文件上传到阿里云OSS, 并获取其上传文件的访问路径
        String url = AliyunOSSUtils.upload(aliyunOSSProperties, file.getBytes(), extName);
        return Result.success(url); // 将文件的访问路径返回给前端
    }

}